package com.chinasoft.service.Impl;

import com.chinasoft.mapper.TeacherMapper;
import com.chinasoft.pojo.PageItem;
import com.chinasoft.pojo.TeacherCount;
import com.chinasoft.pojo.TeacherList;
import com.chinasoft.service.TeacherService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class TeacherServiceImpl implements TeacherService {
    TeacherMapper teacherMapper;

    @Override
    public List<TeacherList> findAll() {
        List<TeacherList> list1 = teacherMapper.findAll();
        List<TeacherList> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneHourAgo = now.minusMinutes(30);//减去一个小时
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String dateTime = oneHourAgo.format(formatter);
        for (TeacherList teacher : list1) {
            int number = Integer.parseInt(teacher.getNumber());
            int soild = Integer.parseInt(teacher.getSoild());
            if ("0".equals(teacher.getDel()) &&"团课".equals(teacher.getClasstype()) && (teacher.getTime().compareTo(dateTime) < 0 || number <= soild)) {
                //当del=0且时间小于dateTime或者人数已满时
                String del = "1";
                teacherMapper.updateDel(teacher.getId(), del);
            }
            result.add(teacher);
        }
        return result;
    }

    @Override
    public TeacherList getById(Integer id) {
        return teacherMapper.getById(id);
    }

    @Override
    public List<TeacherList> getBy(TeacherList list) {
        return teacherMapper.findBy(list);
    }

    @Override
    public List<TeacherList> getfind(TeacherList list) {

        return teacherMapper.find1(list);
    }

    @Override
    public List<TeacherList> getByClass(String classtype) {
        List<TeacherList> list = teacherMapper.getByClass(classtype);
        List<TeacherList> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneHourAgo = now.minusMinutes(10);//减去10分钟
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String dateTime = oneHourAgo.format(formatter);
        for (TeacherList teacher : list){
            int number = Integer.parseInt(teacher.getNumber());
            int soild = Integer.parseInt(teacher.getSoild());
            if("0".equals(teacher.getDel()) && "团课".equals(teacher.getClasstype()) && (teacher.getTime().compareTo(dateTime) < 0 || number <= soild)) {
                //当del=0且时间小于dateTime或者人数已满时
                String del = "1";
                teacherMapper.updateDel(teacher.getId(),del);
            }
            if("0".equals(teacher.getDel()) && "1".equals(teacher.getState())){
                result.add(teacher);
            }
        }
        return result;
    }

    @Override
    public PageItem<TeacherList> getByState(String state,Integer current,Integer size) {
        List<TeacherList> list = teacherMapper.getByState(state);
        List<TeacherList> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneHourAgo = now.minusMinutes(10);//减去一个小时
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String dateTime = oneHourAgo.format(formatter);
        for (TeacherList teacher : list){
            int number = Integer.parseInt(teacher.getNumber());
            int soild = Integer.parseInt(teacher.getSoild());
            if("0".equals(teacher.getDel()) && "团课".equals(teacher.getClasstype()) && (teacher.getTime().compareTo(dateTime) < 0 || number <= soild) ){
                //当del=0且时间小于dateTime或者人数已满时
                String del = "1";
                teacherMapper.updateDel(teacher.getId(),del);
            }
            if("0".equals(teacher.getDel())){
                result.add(teacher);
            }
        }
        int start = (current - 1) * size;
        int end = Math.min(start + size, result.size());
        List<TeacherList> pageResult = result.subList(start, end);
        // 返回分页结果
        long total = result.size();
        PageItem<TeacherList> pi = new PageItem<>(total, pageResult);
        return pi;
    }

    @Override
    public List<TeacherList> getByTeacher(String teachername) {
        List<TeacherList> list = teacherMapper.getByTeacher(teachername);
        List<TeacherList> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneHourAgo = now.minusMinutes(10);//减去一个小时
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String dateTime = oneHourAgo.format(formatter);
        for (TeacherList teacher : list){
            int number = Integer.parseInt(teacher.getNumber());
            int soild = Integer.parseInt(teacher.getSoild());
            if("0".equals(teacher.getDel()) && "团课".equals(teacher.getClasstype()) && (teacher.getTime().compareTo(dateTime) < 0 ||  number <= soild ) ){
                //当del=0且时间小于dateTime或者人数已满时
                String del = "1";
                teacherMapper.updateDel(teacher.getId(),del);
            }
            if("0".equals(teacher.getDel())){
                result.add(teacher);
            }
        }
        return result;
    }

    @Override
    public List<TeacherList> getByTeacherAll(String teachername) {
        List<TeacherList> list = teacherMapper.getByTeacher(teachername);
        List<TeacherList> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneHourAgo = now.minusMinutes(10);//减去一个小时
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String dateTime = oneHourAgo.format(formatter);
        for (TeacherList teacher : list){
            int number = Integer.parseInt(teacher.getNumber());
            int soild = Integer.parseInt(teacher.getSoild());
            if("0".equals(teacher.getDel()) && "团课".equals(teacher.getClasstype()) && (teacher.getTime().compareTo(dateTime) < 0 || number <= soild) ){
                //当del=0且时间小于dateTime或者人数已满时
                String del = "1";
                teacherMapper.updateDel(teacher.getId(),del);
            }
            result.add(teacher);
        }
        return result;
    }

    @Override
    public boolean updateSoild(Integer id, String soild) {
        int i = teacherMapper.updateStatusByOrderCode(id, soild);
        if (i ==1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean updateState(Integer id, String state) {
        int i = teacherMapper.updateState(id,state);
        if(i == 1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<TeacherCount> findgetNum() {
        return teacherMapper.findbytype();
    }

    @Override
    public List<TeacherCount> find() {
        return teacherMapper.find();
    }

    @Override
    public boolean addlist(TeacherList userlist) {
        int i = teacherMapper.insert(userlist);
        if(i == 1){
            return true;
        } else{
            return false;
        }
    }

    @Override
    public PageItem<TeacherList> find1(Integer current, Integer size) {
        // 先获取所有数据
        List<TeacherList> allList = teacherMapper.findAll();
        List<TeacherList> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneHourAgo = now.minusMinutes(10);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String dateTime = oneHourAgo.format(formatter);

        // 筛选符合条件的数据
        for (TeacherList teacher : allList) {
            int number = Integer.parseInt(teacher.getNumber());
            int soild = Integer.parseInt(teacher.getSoild());
            if ("0".equals(teacher.getDel()) && "团课".equals(teacher.getClasstype()) && (teacher.getTime().compareTo(dateTime) < 0 || number <= soild)) {
                String del = "1";
                teacherMapper.updateDel(teacher.getId(), del);
            }
            if ("0".equals(teacher.getDel()) || "1".equals(teacher.getState())) {
                result.add(teacher);
            }
        }
        // 对筛选结果进行分页
        int start = (current - 1) * size;
        int end = Math.min(start + size, result.size());
        List<TeacherList> pageResult = result.subList(start, end);
        // 返回分页结果
        long total = result.size();
        PageItem<TeacherList> pi = new PageItem<>(total, pageResult);
        return pi;
    }

    @Override
    public Boolean deleteById(Integer id) {
        String del = "1";
        int i = teacherMapper.updateDel(id,del);
        if(i == 1){
            return true;
        } else{
            return false;
        }
    }

    @Override
    public Boolean delete1(Integer id, String classtype) {
        String del= "1";
        int i = teacherMapper.updateDel(id,del);
        if("团课".equals(classtype)){
            int s = teacherMapper.update1(id,del);
            if(s > 0){
                return true;
            } else{
                return false;
            }
        }else {

            if(i == 1){
                return true;
            } else{
                return false;
            }
        }
    }

    @Override
    public boolean update(TeacherList teacherList) {
        int i = teacherMapper.update(teacherList);
        if(i == 1){
            return true;
        } else{
            return false;
        }
    }

    @Override
    public boolean getTime(String teachername, String classtype, String time) {
        Integer num = teacherMapper.getTime(teachername,classtype,time);
        if(num == 1){
            return true;
        } else{
            return false;
        }
    }
//    @Override
//    public List<User> findAll() {
//        return userMapper.findAll();
//    }

}
